<template>
  <div class="order">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <!-- 订单号 -->
        <el-form-item label="订单号">
          <el-input
            v-model="formInline.orderNo"
            placeholder="订单号"
          ></el-input>
        </el-form-item>
        <!-- 收货人 -->
        <el-form-item label="收货人">
          <el-input
            v-model="formInline.consignee"
            placeholder="收货人"
          ></el-input>
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item label="手机号">
          <el-input v-model="formInline.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <!-- 订单状态 -->
        <el-form-item label="订单状态">
          <el-select v-model="formInline.orderState" placeholder="订单状态">
            <el-option label="已完成" value="已完成"></el-option>
            <el-option label="进行中" value="进行中"></el-option>
            <el-option label="派送中" value="派送中"></el-option>
          </el-select>
          <!-- 选择时间 -->
        </el-form-item>
        <el-form-item label="选择时间">
          <el-date-picker
            v-model="formInline.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <!-- 查询 -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" border style="width: 100%">
        <!-- 订单号 -->
        <el-table-column fixed prop="orderNo" label="订单号" width="150">
        </el-table-column>
        <!-- 下单时间 -->
        <el-table-column label="下单时间" width="120">
          <template slot-scope="scope">{{
            scope.row.orderTime | normalizeHms
          }}</template>
        </el-table-column>
        <!-- 手机号 -->
        <el-table-column prop="phone" label="手机号" width="120">
        </el-table-column>
        <!-- 收货人 -->
        <el-table-column prop="consignee" label="收货人" width="120">
        </el-table-column>
        <!-- 配送地址 -->
        <el-table-column prop="deliverAddress" label="配送地址" width="300">
        </el-table-column>
        <!-- 送达时间 -->
        <el-table-column label="送达时间" width="120">
          <template slot-scope="scope">{{
            scope.row.deliveryTime | normalizeHms
          }}</template>
        </el-table-column>
        <!-- 用户备注 -->
        <el-table-column prop="remarks" label="用户备注" width="120">
        </el-table-column>
        <!-- 订单金额 -->
        <el-table-column prop="orderAmount" label="订单金额" width="120">
        </el-table-column>
        <!-- 订单状态 -->
        <el-table-column prop="orderState" label="订单状态">
        </el-table-column>
        <!-- 操作 -->
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <!-- 查看 -->
            <el-button
              @click="
                handleClick(scope.row);
                dialog = true;
              "
              type="text"
              size="small"
              >查看</el-button
            >
            <!-- 编辑 -->
            <el-button type="text" size="small" @click="edit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagin.currentPage"
        :page-sizes="[3, 5, 10]"
        :page-size="pagin.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      title="订单管理编辑"
      :before-close="handleClose"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
    >
      <div class="demo-drawer__content">
        <el-form :model="form" label-width="120px">
          <el-form-item label="订单号:">
            {{ form.orderNo }}
          </el-form-item>
          <el-form-item label="下单时间:">
            {{ form.orderTime | normalizeHms }}
          </el-form-item>
          <el-form-item label="联系电话:">
            {{ form.phone }}
          </el-form-item>
          <el-form-item label="收货人:">
            {{ form.consignee }}
          </el-form-item>
          <el-form-item label="送货地址:">
            {{ form.deliverAddress }}
          </el-form-item>
          <el-form-item label="送达时间:">
            {{ form.deliveryTime | normalizeHms }}
          </el-form-item>
          <el-form-item label="备注:">
            {{ form.remarks }}
          </el-form-item>
          <el-form-item label="订单金额:">
            {{ form.orderAmount }}
          </el-form-item>
          <el-form-item label="订单状态:">
            {{ form.orderState }}
          </el-form-item>
          <el-form-item label-width=""> </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { orderList, orderderail } from '@/api/order';
export default {
  data () {
    return {
      //当前页数据
      pagin: {
        currentPage: 1,
        pageSize: 3
      },
      // 查询数据
      formInline: {
        orderNo: '',
        consignee: '',
        orderState: '',
        date: '',
      },
      // 表格数据
      tableData: [],
      //总条数
      total: 0,
      //点击查询按钮才把查询相关的数据存到这里面
      searchForm: {},
      dialog: false,
      loading: false,
      form: {},
    }
  },
  methods: {
    // 获取数据
    async getData (searchFrom = {}) {
      // 解构当前数据 合并一起
      let res = await orderList({ ...this.pagin, ...searchFrom })
      // 解构数据 然后赋值
      let { data, total } = res.data
      if (data.length == 0 && this.pagin.currentPage != 1) {
        this.pagin.currentPage -= 1
        this.getData()
        return
      }
      //渲染数据
      this.tableData = data
      // 渲染总条
      this.total = total
    },
    // 点击查看
    async handleClick (row) {
      let res = await orderderail({ id: row.id })
      let { data } = res.data
      this.form = { ...data }
    },
    // 查询
    onSubmit () {
      //判断formInline里面有没有值 如果没有值,就不执行下面的查询
      let flag = Object.keys(this.formInline).some(v => {
        return this.formInline[v]
      })
      if (!flag) return
      // 返回到第一页
      this.currentPage = 1
      //把formInline里面的数据 浅拷贝一份给searchForm存储起来
      this.searchForm = { ...this.formInline }
      //处理数据格式
      let date = JSON.stringify(this.searchForm.date)
      this.getData({ ...this.searchForm, date })
    },
    // 当前页
    handleCurrentChange (val) {
      this.pagin.currentPage = val
      let date = JSON.stringify(this.searchForm.date)
      this.getData({ ...this.searchForm, date })
    },
    // 当前页条
    handleSizeChange (val) {
      this.pagin.pageSize = val
      let date = JSON.stringify(this.searchForm.date)
      this.getData({ ...this.searchForm, date })
    },
    handleClose () {
      if (this.loading) {
        return;
      }
      this.loading = false;
      this.dialog = false;
      clearTimeout(this.timer);
    },
    edit (row) {
      console.log('row :>> ', row);
      this.$router.push({ path: '/order/order-edit', query: { id: row } })
    }
  },
  created () {
    //初始化渲染
    this.getData();
  },

}
</script>

<style lang="less" scoped>
</style>